let fs = require('fs')
let path = require('path')
const utils = require('../../utils')
let express = require('express')
const curd = require('../../curd')

let router = express.Router()

const COLUMN_DATA_PATH = './src/db/column.json';

module.exports = [
	{
		path: '/queryColumnByCode',
		request: function ( req, res ) {
			if( !req.tableCode ) {
				res.send(
					utils.resultError('缺少code')
				).end()
			} else {
				let data = JSON.parse(fs.readFileSync(path.resolve(COLUMN_DATA_PATH), 'utf-8'))
				let columns = data.filter(it => it.tableCode === req.tableCode)?.[0].columns || []
				res.send(
					utils.resultSuccess(columns)
				).end()
			}
		}
	},
	{
		path: '/queryPage',
		request: function ( req, res ) {
			res.send(
				curd.getPages(COLUMN_DATA_PATH, req)
			).end()
		}
	},
	{
		path: '/deleteByIds',
		request: function ( req, res ) {
			res.send(
				curd.deletes(COLUMN_DATA_PATH, req)
			).end()
		}
	},
	{
		path: '/updateById',
		request: function ( req, res ) {
			res.send(
				curd.update(COLUMN_DATA_PATH, req)
			).end()
		}
	},
	{
		path: '/add',
		request: function ( req, res ) {
			res.send(
				curd.add(COLUMN_DATA_PATH, {
					...req,
					columns: []
				})
			).end()
		}
	},
	{
		path: '/queryById',
		request: function ( req, res ) {
			res.send(
				curd.queryById(COLUMN_DATA_PATH, req.id)
			).end()
		}
	},

	{
		path: '/queryColumnPage',
		request: function ( req, res ) {
			let data = JSON.parse(fs.readFileSync(path.resolve(COLUMN_DATA_PATH), 'utf-8'))
			let columns = data.filter(it => it.id === req.tableId)?.[0].columns || []

			res.send(
				utils.resultPageSuccess(req.pageNumber - 1, req.pageSize, columns)
			).end()
		}
	},
	{
		path: '/addColumn',
		request: function ( req, res ) {
			let data = JSON.parse(fs.readFileSync(path.resolve(COLUMN_DATA_PATH), 'utf-8'))
			let obj = data.filter(it => it.id === req.tableId)?.[0]

			obj.columns.push( {
				...req,
				id: curd.createId()
			} )

			res.send(
				curd.update(COLUMN_DATA_PATH, obj)
			).end()
		}
	},
	{
		path: '/updateColumn',
		request: function ( req, res ) {
			let data = JSON.parse(fs.readFileSync(path.resolve(COLUMN_DATA_PATH), 'utf-8'))
			let obj = data.filter(it => it.id === req.tableId)?.[0]

			obj.columns.forEach( (row, index) => {
				if(row.id === req.id) {
					delete req.tableId
					obj.columns[index] = { ...row, ...req }
				}
			} )

			res.send(
				curd.update(COLUMN_DATA_PATH, obj)
			).end()
		}
	},

	// 删除columns
	{
		path: '/delColumn',
		request: function ( req, res ) {
			let data = JSON.parse(fs.readFileSync(path.resolve(COLUMN_DATA_PATH), 'utf-8'))
			let obj = {}

			data.forEach( row => {
				let column = row.columns.filter( column => req.indexOf(column.id) !== -1 )

				if( column.length ) {
					obj = { ...row }
					obj.columns = obj.columns.filter( column => req.indexOf(column.id) === -1 )
				}
			} )

			res.send(
				curd.update(COLUMN_DATA_PATH, obj)
			).end()
		}
	},
	{
		path: '/queryColumnById',
		request: function ( req, res ) {
			let data = JSON.parse(fs.readFileSync(path.resolve(COLUMN_DATA_PATH), 'utf-8'))
			let hasGetRow = false
			let rowColumn = { }

			data.forEach( row => {
				if( hasGetRow ) return

				row.columns.forEach( column => {
					if(column.id === req.id) {
						hasGetRow = true
						rowColumn = { ...column }
					}
				} )
			} )

			res.send(
				utils.resultSuccess( rowColumn )
			).end()
		}
	},
]
